<?php
namespace Cyfy\Modules\Image;

/**
 * Image resize class
 *
 * Example:
 * $image = new Image('obrazek.png');
 * $image -> resize(800, 600);
 * $image -> save();
 * $image -> close();
 *
 * @category   cyfy
 * @package    libraries
 * @subpackage image
 * @version    Release: 1.0
 */

class Image
{
    private $image, $source, $type = 'image/jpeg', $dstfct = 'imagejpeg';
    
    /**
     * Is the file image?
     */
    static public function isImage($filename)
    {
        $path_info = pathinfo($filename);
        if(in_array(strtolower($path_info['extension']), array('jpg', 'jpeg', 'pjpeg', 'png', 'x-png', 'gif'))){
            return true;
        }
        return false;
    }
    
    /**
     * Constructor can load a file
     * @param string $file Filename
     */
    public function __construct($file = '')
    {
        if ($file!='') {
            $this -> load($file);
        }
    }
  
    /**
     * Loads a file
     * @param string $s Filename
     * @param string $type Type of the file. Not required
     * @return bool
     */
	public function load($s, $type = '')
    {
		$this -> source = $s;
        if (!$type) {
            $info = pathinfo(realpath($s));
            $ext = $info["extension"];
        } else {
            $ext = substr($type, 6, strlen($type));
        }
       
        switch (strtolower($ext)) 
		{
			case 'jpg':
			case 'jpeg':
			case 'pjpeg':
				$srcfct = 'imagecreatefromjpeg';
				$this -> dstfct = 'imagejpeg';
				$this -> type = 'image/jpeg';
				break;
			case 'png':
			case 'x-png':
				$srcfct = 'imagecreatefrompng';
				$this -> dstfct = 'imagepng';
				$this -> type = 'image/png';
				break;
			case 'gif':
				$srcfct = 'imagecreatefromgif';
				$this -> dstfct = 'imagegif';
				$this -> type = 'image/gif';
				break;
			default:
				return false;
		}
    
        $this -> source = $s;
        $this -> image = $srcfct($s);
        return true;
    }
  
	/**
     * Resizes the image
     * @param  integer   $x          Maximum width
     * @param  integer   $y          Maximum height
     * @param  bool      $maximize   When true, the image will fill the place.
     * @return bool
     */  
	public function resize($max_x, $max_y, $maximize = false){
		
		$src_x = imagesx($this->image); // source width
		$src_y = imagesy($this->image); // source height
		
        if ($src_x > $max_x || $src_y > $max_y || $maximize)
		{
            $source_image = $this -> image;
            
            if ($maximize == true){
                $width = $max_x;
                $height = $max_y;
            } else {
                $width = $max_x;
                $height = Ceil($src_y/($src_x/$max_x));
                if ($height>$max_y) {
                    $height = $max_y;
                    $width = Ceil($src_x/($src_y/$max_y));
                }
            }
			
            $this -> image = ImageCreateTrueColor($width,$height);
            ImageCopyResampled($this -> image,$source_image,0,0,0,0,$width,$height,$src_x,$src_y);
            return true;
		}
		else 
		{
            return false;
        }
	}

	/**
     * Shows the image
     * @param  integer   $jpegquality    JPEG quality if shown as JPEG.
     * @return bool
     */  
	public function view($jpegquality = 100)
    {
		header("Content-type: {$this -> type}");
        $dstfct = $this -> dstfct;
        if ($jpegquality && $this->type == 'image/jpeg')
		{
			return $dstfct($this->image, "", $jpegquality);
        }
		else
		{
			return $dstfct($this->image);
        }
    }

	/**
     * Saves the image to another file
     * @param  string   $destination  New file path
     * @param  integer  $jpegquality  JPEG quality if shown as JPEG.
     * @return bool
     */
	public function saveAs($destination,$jpegquality = 100)
    {
        $dstfct = $this -> dstfct;
        if ($jpegquality&&$this->type=="image/jpeg") 
		{
			return $dstfct($this->image, $destination, $jpegquality);
        }
		else
		{
			return $dstfct($this->image, $destination);
        }
    }

	/**
     * Saves the image to the same file
     * @see saveAs()
     * @param  integer   $jpegquality    JPEG quality if shown as JPEG.
     */
	public function save($jpegquality = 100)
    {
		$this -> saveAs($this->source,$jpegquality);
	}
    
    /**
     * Empty the memory
     */
    public function close()
    {
		ImageDestroy($this -> image);
	}
}
